package com.learn.xueer.app.util

import android.text.TextUtils
import androidx.collection.arraySetOf
import com.blankj.utilcode.util.GsonUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.learn.xueer.data.model.bean.LoginResponse
import com.learn.xueer.data.model.bean.TextBookResponse
import com.learn.xueer.data.model.bean.UserInfoResponse
import com.learn.xueer.data.model.bean.WordInfoResponse
import com.tencent.mmkv.MMKV

object CacheUtil {

    /**
     * 获取保存的账户信息
     */
    fun getUser(): UserInfoResponse? {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("user")
        return if (TextUtils.isEmpty(userStr)) {
            null
        } else {
            Gson().fromJson(userStr, UserInfoResponse::class.java)
        }
    }

    fun clearAll() {
        setToken("")
    }

    /**
     * 设置账户信息
     */
    fun setUser(userResponse: UserInfoResponse?) {
        val kv = MMKV.mmkvWithID("app")
        if (userResponse == null) {
            kv.encode("user", "")
            setIsLogin(false)
        } else {
            kv.encode("user", Gson().toJson(userResponse))
            setIsLogin(true)
        }

    }

    /**
     * 是否已经登录
     */
    fun isLogin(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("login", false)
    }

    fun setToken(token: String) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("token", token)
    }

    fun insertLoginAccount(account: String, pass: String) {
        val kv = MMKV.mmkvWithID("app")
        var accountList = getLoginAccount()
        var filter = accountList.find {
            it.account == account
        }

        if (filter == null)
            accountList.add(User(account, pass))
        else {
            accountList.remove((filter))
            accountList.add(User(account, pass))
        }

        accountList = accountList.toSet().toMutableList()
        kv.encode("account", Gson().toJson(accountList))
    }

    fun deleteLoginAccount(user: User) {
        val kv = MMKV.mmkvWithID("app")
        var accountList = getLoginAccount()
        var index = accountList.indexOfFirst {
            it.account == user.account
        }
        accountList.removeAt(index)
        kv.encode("account", Gson().toJson(accountList))
    }

    fun getLoginAccount(): MutableList<User> {
        val kv = MMKV.mmkvWithID("app")
        var decodeString = kv.decodeString("account", "")

        if (decodeString.isNullOrEmpty()) {
            return arrayListOf()
        }
        return Gson().fromJson<ArrayList<User>>(
            decodeString,
            object : TypeToken<ArrayList<User>>() {}.type
        )
    }


    fun getToken(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("token", "")
    }

    fun isCareRye(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("care", false)
    }

    fun setIsCareEye(isCareRye: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("care", isCareRye)
    }

    /**
     * 设置是否已经登录
     */
    fun setIsLogin(isLogin: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("login", isLogin)
    }

    /**
     * 是否是第一次登陆
     */
    fun isFirst(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("first", true)
    }

    /**
     * 是否是第一次登陆
     */
    fun setFirst(first: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("first", first)
    }

    /**
     * 首页是否开启获取指定文章
     */
    fun isNeedTop(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("top", true)
    }

    /**
     * 设置首页是否开启获取指定文章
     */
    fun setIsNeedTop(isNeedTop: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("top", isNeedTop)
    }

    fun setFontSize(fontScale: Float): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("fontSize", fontScale)
    }

    fun getFontSize(): Float {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeFloat("fontSize", 1.0f)
    }

    fun getPlay(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("play", false)
    }

    fun getWith(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("with", false)
    }

    fun getSound(): Float {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeFloat("sound", 1.0f)
    }

    fun setSound(sound: Float): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("sound", sound)
    }

    fun setPlay(play: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("play", play)
    }

    fun setWithRead(with: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("with", with)
    }

    /**
     * 获取搜索历史缓存数据
     */
    fun getSearchHistoryData(): ArrayList<String> {
        val kv = MMKV.mmkvWithID("cache")
        val searchCacheStr = kv.decodeString("history")
        if (!TextUtils.isEmpty(searchCacheStr)) {
            return Gson().fromJson(searchCacheStr, object : TypeToken<ArrayList<String>>() {}.type)
        }
        return arrayListOf()
    }

    fun setSearchHistoryData(searchResponseStr: String) {
        val kv = MMKV.mmkvWithID("cache")
        kv.encode("history", searchResponseStr)
    }

    /**
     * 获取保存的账户信息
     */
    fun getTextBook(): TextBookResponse? {
        val kv = MMKV.mmkvWithID("app")
        val textBookStr = kv.decodeString("textBook")
        return if (TextUtils.isEmpty(textBookStr)) {
            null
        } else {
            Gson().fromJson(textBookStr, TextBookResponse::class.java)
        }
    }

    /**
     * 设置账户信息
     */
    fun setTextBook(textBookResponse: TextBookResponse?) {
        val kv = MMKV.mmkvWithID("app")
        if (textBookResponse == null) {
            kv.encode("textBook", "")
        } else {
            kv.encode("textBook", Gson().toJson(textBookResponse))
        }
    }
}

class User(var account: String, var pass: String)
